package com.augurit.common.system.web.org;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.form.BaseForm;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IOmOrgService;
import com.augurit.common.system.service.org.IOmPositionService;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.util.ExtUtils;

/**
 * 岗位管理Action类
 */
@SuppressWarnings("serial")
public class OmPositionAction extends ExtCrudActionSupport<OmPositionForm> {

	// Service 引用
	@Autowired
	private IOmOrgService omOrgService;
	
	@Autowired
	private IOmPositionService omPositionService;
	
	@Autowired
	private IAcRoleService acRoleService;
	

	// 基本属性
	private Page<OmPositionForm> page = new Page<OmPositionForm>(15);
	private OmPositionForm form;

	private Long orgId;
	private Long groupId;
	
	private Long posId;
	private Long[] posIds;
	
	private Long roleId;
	private Long[] roleIds;

	
	/**
	 * 条件查询岗位信息
	 */
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 按条件查询机构
	 */
	public String getPosByOrgId() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		omOrgService.getPositionsByOrg(orgId, page);
		this.extRenderGridJson(page);
		return null;
	}
	
	public String getPosByGroupId() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		omOrgService.getPositionsByGroup(groupId, page);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 根据父岗位id获取下级岗位
	 */
	public String getPosByParentPosId() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		omPositionService.getChildPositions(posId, page);		
		this.extRenderGridJson(page);
		return null;
	}
		
	/**
	 * 新增或编辑岗位页面
	 */
	@Override
	public String input() throws Exception {
		this.renderOmPositionFormJson(form);
		return null;
	}
	
	@Override
	public String save() throws Exception {
		omPositionService.savePosCascade(form);
		this.extRenderSuccess();
		return null;
	}

	/**
	 * 将Form对象转换为Ext的FormPanel组件所需的JSON字符串
	 * @param form
	 * @param headers
	 */
	public static void renderOmPositionFormJson(final BaseForm form, final String... headers) {
		String json = extGetFormJson(form);
		json = json.replace("\"users\":[],", "");
		renderJson(json, headers);
	}

	/**
	 * 删除一个或多个岗位
	 */
	@Override
	public String delete() throws Exception {
		if(posIds != null && posIds.length > 0){
			for(Long posId : posIds)
				this.omPositionService.deletePosition(posId);
		}
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 王剑文添加
	 * 获取与指定角色关联的岗位
	 * @return
	 * @throws Exception
	 */
	public String getPosesByRole() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acRoleService.getPositionsByRole(page, roleId);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 王剑文添加
	 * 获取与不与指定角色关联的岗位
	 * @return
	 * @throws Exception
	 */
	public String getPosesNotAssociateRole() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acRoleService.getPositionsNotAssociateRole(page, roleId, form);
		this.extRenderGridJson(page);
		return null;
	}


	public OmPositionForm getModel(){
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(posId != null)
			form = omPositionService.get(posId);
		else
			form = new OmPositionForm();
	}
	
	// 属性getter和setter方法
	public Page<OmPositionForm> getPosPage() {
		return page;
	}

	public OmPositionForm getOmPositionForm() {
		return form;
	}

	public void setOmPositionForm(OmPositionForm omPositionForm) {
		this.form = omPositionForm;
	}

	public Long getPosId() {
		return posId;
	}

	public void setPosPage(Page<OmPositionForm> page) {
		this.page = page;
	}

	public void setPosId(Long posId) {
		this.posId = posId;
	}

	public Long[] getPosIds() {
		return posIds;
	}

	public void setPosIds(Long[] posIds) {
		this.posIds = posIds;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
}
